package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.WorkDao;
import com.bdqn.oa.entity.Work;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author qiaolu
 * @date 2022/7/20 16:11
 */
public class WorkDaolmpl implements WorkDao {
    private QueryRunner queryRunner= new QueryRunner();
    /**
     * 通过workid查询
     *
     * @param workid
     * @return
     * @throws SQLException
     */
    @Override
    public Work getbyidWork(Integer workid) throws SQLException {
        Work work = null;
        try {
            String sql = "select * from work where id = ?";
        work = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Work>(Work.class),workid);
        }catch (Exception e){
            e.printStackTrace();
        }

        return work;
    }

    /**
     * 查询全部工作
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Work> listAllWorky() throws SQLException {
        List<Work> workList = null;
        try {
            String sql = "select * from work";
            workList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Work>(Work.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return workList;
    }

    /**
     * 增加工作
     *
     * @param work
     * @return
     * @throws SQLException
     */
    @Override
    public int saveWork(Work work) throws SQLException {
        String sql = "insert into work(position_id,type,content,iscore)values(?,?,?,?)";
        return queryRunner.update(DruidUtils.getConnection(),sql,work.getPositionId(),work.getType(),work.getContent(),work.getIscore());
    }

    /**
     * 修改工作
     *
     * @param work
     * @return
     * @throws SQLException
     */
    @Override
    public int updateWork(Work work) throws SQLException {
        String sql ="update work set type=?,content=?,iscore=? where id= ?";

        return queryRunner.update(DruidUtils.getConnection(),sql,work.getType(),work.getContent(),work.getIscore(),work.getId());
    }
}
